clear all

cd "_______________"

// This do file produces the LASSO results with the manipulation measures using the alternative manipulation window.
// These results are reported in the Online Appendix, Tables A10-A12

log using "log_LASSO_results.log", replace
use "PM10_locoff_befandaft_merge_alt.dta", clear
drop temp

by id year, sort: gen temp=_n 
drop if temp>1
drop temp


xtset id year 

rename posranking posrk
qui tab posrk, gen(posrk)

// define secretary variables
gen match_pr=(posprovince==birthprovince)
gen colgnb=(edu_ft_entertime>=197100 & edu_ft_entertime<197800)
gen col78=(edu_ft_entertime>=197800 & edu_ft_entertime<197900) // first two cohorts after wenge
gen age=year-floor(birthday/100)
gen yearstore=57-age  //year left to be possibly promoted to the next step..
replace yearstore=62-age if posrk==5 
replace yearstore=67-age if posrk==6 
replace yearstore=0 if yearstore<0
gen yeartoredum1=(yearstore==0)
gen yeartoredum2=(yearstore<=5&yearstore>0 )
gen yeartoredum3=(yearstore>5 )
gen ft_col=(edu_ft_highest==3|edu_ft_highest==4)
gen ft_stem=(edu_ft_major==2 | edu_ft_major==3)
gen pt1=(edu_pt_highest<=3& edu_pt_highest!=.)
gen pt2=(edu_pt_highest>=4& edu_pt_highest!=.)
gen  ft_key=(edu_ft_key1==1|edu_ft_key2==1)
gen yearsinpos=year-floor(posstart/100)
rename befcitypartysecretary befcitysec
rename befcountypartysecretary befctysec
rename befcountymayor befctymayor
rename befotherprovince befotherprov
rename befyearsinprovince befyearsinpr
replace befyearsinpr=1*(befyearsinpr>0)
replace befcentralgov=1*(befcentralgov>0)
replace befyearsincity=1*(befyearsincity>0)
replace befotherprov=1*(befotherprov>0)

// define mayor variables
gen m_match_pr=(mayor_posprovince==mayor_birthprovince)
gen m_colgnb=(mayor_edu_ft_entertime>=197100 & mayor_edu_ft_entertime<197800)
gen m_col78=(mayor_edu_ft_entertime>=197800 & mayor_edu_ft_entertime<197900) // first two cohorts after wenge
gen m_age=year-floor(mayor_birthday/100)
gen m_yearstore=57-m_age  //year left to be possibly promoted to the next step..
replace m_yearstore=62-m_age if posrk==5 
replace m_yearstore=67-m_age if posrk==6
replace m_yearstore=0 if m_yearstore<0
gen m_yeartoredum1=(m_yearstore==0)
gen m_yeartoredum2=(m_yearstore<=5&m_yearstore>0 )
gen m_yeartoredum3=(m_yearstore>5 )
gen m_ft_col=(mayor_edu_ft_highest==3|mayor_edu_ft_highest==4)
gen m_ft_stem=(mayor_edu_ft_major=="2"| mayor_edu_ft_major=="3")
gen m_pt1=(mayor_edu_pt_highest<=3& mayor_edu_pt_highest!=.)
gen m_pt2=(mayor_edu_pt_highest>=4& mayor_edu_pt_highest!=.)
gen  m_ft_key=(mayor_edu_ft_key1==1|mayor_edu_ft_key2==1)
replace  m_ft_key=0 if  m_ft_key==.
gen m_yearsinpos=year-floor(mayor_posstart/100)
rename mayor_posprovcpc m_posprovcpc
rename mayor_befyearsinprovince m_befyearsinpr
replace m_befyearsinpr=(m_befyearsinpr>0)
replace mayor_befcentralgov=(mayor_befcentralgov>0)
rename mayor_male m_male
rename mayor_han m_han
rename mayor_befcitypartysecretary m_befcitysec
rename mayor_befcitymayor m_befcitymayor
rename mayor_befsecretary  m_befsecretary
rename mayor_befenterprise m_befenterprise 
rename mayor_befresearch m_befresearch 
rename mayor_befcentralgov m_befcentralgov 
rename mayor_befyearsincity m_befyearsincity
replace m_befyearsincity=(m_befyearsincity>0)
rename mayor_befcountypartysecretary m_befctysec
rename mayor_befcountymayor m_befctymayor
rename mayor_befotherprovince m_befotherprov
replace m_befotherprov=(m_befotherprov>0)
qui by id name, sort: gen sec_t=_n
qui by id mayor_name, sort: gen mayor_t=_n


// define mutually exclusive demographic variables
gen malehan=male*han
gen femalehan=(1-male)*han
gen malenonhan=male*(1-han)
gen m_malehan=m_male*m_han
gen m_femalehan=(1-m_male)*m_han
gen m_malenonhan=m_male*(1-m_han)


global ctr_sec_demo="malehan femalehan malenonhan" 
global ctr_sec_poly="yearsinpos yearstore match_pr befenterprise befresearch befsecretary befctymayor befctysec befcitymayor befcitysec befcentralgov befyearsincity befyearsinpr befotherprov"

global ctr_sec_edu="ft_col ft_stem ft_key colgnb col78 pt1 pt2"

global ctr_mayor_demo="m_malehan m_femalehan m_malenonhan" 
global ctr_mayor_poly="m_yearsinpos m_yearstore m_match_pr m_befenterprise m_befresearch m_befsecretary m_befctymayor m_befctysec m_befcitymayor m_befcitysec m_befcentralgov m_befyearsincity m_befyearsinpr m_befotherprov"

global ctr_mayor_edu="m_ft_col m_ft_stem m_ft_key m_colgnb m_col78 m_pt1 m_pt2"

// TABLE 3: Summary Statistics Table: Secretaries and Mayors by CityxYear Obs (Columns (1) & (2))
// Summary Statistics by CityxOfficial Obs (Columns (3) & (4)) are obtained using "Sec_and_Mayor_Characteristics_indivcity.do"
tabstat $ctr_sec_demo $ctr_sec_edu $ctr_sec_poly, statistics(mean sd count) save
return list
matrix PSStats=r(StatTotal)'
putexcel set SumStats_Sec, replace
putexcel A2=matrix(PSStats),rownames
tabstat $ctr_mayor_demo $ctr_mayor_edu $ctr_mayor_poly, statistics(mean sd count) save
return list
matrix MayorStats=r(StatTotal)'
putexcel set SumStats_Mayor, replace
putexcel A2=matrix(MayorStats),rownames


// exclude befyearsinpr and m_befyearsinpr from LASSO since they have very little variation
global ctr_sec_poly="yearsinpos yearstore match_pr befenterprise befresearch befsecretary befctymayor befctysec befcitymayor befcitysec befcentralgov befyearsincity befotherprov"
global ctr_mayor_poly="m_yearsinpos m_yearstore m_match_pr m_befenterprise m_befresearch m_befsecretary m_befctymayor m_befctysec m_befcitymayor m_befcitysec m_befcentralgov m_befyearsincity m_befotherprov"


// creating factor variables for province and year for baseline LASSO specification
gen dprovince=posprovince
qui tab dprovince, gen(dprovince)
drop dprovince
*global ctr_prov="province2 province3 province5 province6 province8 province9 province10 province11 province12 province13 province14 province15 province16 province17 province18 province19 province20 province21 province22 province23 province24 province25 province26 province27 province29 province30"
global ctr_province="dprovince2 dprovince3 dprovince5 dprovince6 dprovince8 dprovince9 dprovince10 dprovince11 dprovince12 dprovince13 dprovince14 dprovince15 dprovince16 dprovince17 dprovince18 dprovince19 dprovince20 dprovince21 dprovince22 dprovince23 dprovince24 dprovince25 dprovince26 dprovince27 dprovince29 dprovince30"

qui tab year, gen(time)

qui tab id, gen (cid)
global dumsgroupfe="posrk2 posrk3 posrk4 dprovince* time*"
global dumsfe="time* cid*"

///// BASELINE LASSO Specification
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1)
global Selfe1 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1)
global Selfe2 `r(selected)'

local Selfe1 $Selfe1
local Selfe2 $Selfe2
local Selfe : list Selfe1 | Selfe2
global Selfe `Selfe'


//// Online Appendix Table A10: Panel A
eststo clear
* baseline post-lasso propFx LASSO
eststo:  xtreg propFx $Selfe posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
* city fixed effects propFx LASSO
eststo:  xtreg propFx $Selfe i.year, fe cluster(id)

*baseline post-lasso manipdays LASSO
eststo: xtreg manipdays $Selfe posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
*city fixed effects manipdays LASSO
eststo: xtreg manipdays $Selfe i.year, fe cluster(id)
esttab using postlasso_baseline.tex, replace b(3) noconstant drop(posrk2 posrk3 posrk4 *.year dprovince*)
eststo clear


////// Online Appendix Table A10: Panel A, Robustness Check I of Key College Variable for Party Secretary (LASSO without Key College)
global ctr_sec_edunokey="ft_col ft_stem colgnb col78 pt1 pt2"
*Group FE LASSO 1: witout PS key college variable

lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edunokey ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1)
global Selfer11 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edunokey ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1)
global Selfer12 `r(selected)'

local Selfer11 $Selfer11
local Selfer12 $Selfer12
local Selfer1: list Selfer11 | Selfer12
global Selfer1 `Selfer1'

eststo: xtreg propFx $Selfer1 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer1 i.year, fe cluster(id)
eststo: xtreg manipdays $Selfer1 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer1 i.year, fe cluster(id)
esttab using postlasso_robust1_nokey.tex, replace  b(3) noconstant drop(*.year posrk2 posrk3 posrk4 dprovince*)
eststo clear

////  Online Appendix Table A10: Panel B, Robustness Check II of Key College Variable for Party Secretary (Varying Control Variables) 
global dumsgroupfer21="dprovince* time*"
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfer21) lasiter(1000) verbose(0) fdisplay(1)
global Selfer211 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfer21) lasiter(1000) verbose(0) fdisplay(1)
global Selfer212 `r(selected)'

local Selfer211 $Selfer211
local Selfer212 $Selfer212
local Selfer21: list Selfer211 | Selfer212
global Selfer21 `Selfer21'

global dumsgroupfer22="dprovince*"
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfer22) lasiter(1000) verbose(0) fdisplay(1)
global Selfer221 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfer22) lasiter(1000) verbose(0) fdisplay(1)
global Selfer222 `r(selected)'

local Selfer221 $Selfer221
local Selfer222 $Selfer222
local Selfer22: list Selfer221 | Selfer222
global Selfer22 `Selfer22'

eststo: xtreg propFx $Selfer21 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer21 i.year, fe cluster(id)
eststo: xtreg propFx $Selfer22 dprovince*, re cluster(id)
eststo: xtreg propFx $Selfer22 i.year, fe cluster(id)
esttab using postlasso_robust2_propFx.tex, replace  b(3) noconstant drop(*.year dprovince*)
eststo clear

eststo: xtreg manipdays $Selfer21 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer21 i.year, fe cluster(id)
eststo: xtreg manipdays $Selfer22 dprovince*, re cluster(id)
eststo: xtreg manipdays $Selfer22 i.year, fe cluster(id)

esttab using postlasso_robust2_manipdays.tex, replace  b(2) noconstant drop(*.year dprovince*)
eststo clear

///// Tables A11: LASSO Robustness Check III: Relaxing Penalty Term*****************************
//Penalty=250, 200, 150, 100
//lambda(250)
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(250)
global Selfer311 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(250)
global Selfer312 `r(selected)'

local Selfer311 $Selfer311
local Selfer312 $Selfer312
local Selfer31 : list Selfer311 | Selfer312
global Selfer31 `Selfer31'

//lambda(200)
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(200)
global Selfer321 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(200)
global Selfer322 `r(selected)'

local Selfer321 $Selfer321
local Selfer322 $Selfer322
local Selfer32 : list Selfer321 | Selfer322
global Selfer32 `Selfer32'

//lambda(150)
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(150)
global Selfer331 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(150)
global Selfer332 `r(selected)'

local Selfer331 $Selfer331
local Selfer332 $Selfer332
local Selfer33 : list Selfer331 | Selfer332
global Selfer33 `Selfer33'

//lambda(100)
lassoShooting propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(100)
global Selfer341 `r(selected)'
lassoShooting manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu, ///
controls($dumsgroupfe) lasiter(1000) verbose(0) fdisplay(1) lambda(100)
global Selfer342 `r(selected)'

local Selfer341 $Selfer341
local Selfer342 $Selfer342
local Selfer34 : list Selfer341 | Selfer342
global Selfer34 `Selfer34'



eststo: xtreg propFx $Selfer31 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer31 i.year, fe cluster(id)
eststo: xtreg propFx $Selfer32 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer32 i.year, fe cluster(id)

eststo: xtreg propFx $Selfer33 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer33 i.year, fe cluster(id)

eststo: xtreg propFx $Selfer34 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg propFx $Selfer34 i.year, fe cluster(id)
esttab using postlasso_robust3_propFx.tex, replace b(3) noconstant drop(*.year posrk2 posrk3 posrk4 dprovince*)
eststo clear


eststo: xtreg manipdays $Selfer31 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer31 i.year, fe cluster(id)
eststo: xtreg manipdays $Selfer32 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer32 i.year, fe cluster(id)
eststo: xtreg manipdays $Selfer33 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer33 i.year, fe cluster(id)

eststo: xtreg manipdays $Selfer34 posrk2 posrk3 posrk4 dprovince* i.year, re cluster(id)
eststo: xtreg manipdays $Selfer34 i.year, fe cluster(id)
esttab using postlasso_robust3_manipdays.tex, replace  b(2) noconstant drop(*.year posrk2 posrk3 posrk4 dprovince*)
eststo clear
log close


///// Online Appendix Table A12: LASSO and Elastic Net RESULTS minimizing Information Criteria
///// Note Table A12 only reports the PSEC coefficients obtained from the following regressions.
log using "log_LASSO_SA_checks.log", replace

local ctr_sec_demo $ctr_sec_demo
local ctr_sec_edu $ctr_sec_edu
local ctr_sec_poly $ctr_sec_poly

local ctr_characteristics : list ctr_sec_demo | ctr_sec_edu
local ctr_characteristics: list ctr_characteristics | ctr_sec_poly 
global ctr_characteristics `ctr_characteristics'
local nchar: word count $ctr_characteristics
forvalues pp = 1/`nchar' {
local temp: word `pp' of $ctr_characteristics
xtreg `temp' i.year, fe cluster(id)
predict `temp'feres, e
}

tabstat $ctr_characteristics, statistics(mean sd count) save
return list
matrix PSStats=r(StatTotal)'
putexcel set Sec_Char_sd, replace
putexcel A2=matrix(PSStats),rownames


tabstat *feres, statistics(mean sd count) save
return list
matrix PSStatsres=r(StatTotal)'
putexcel set Sec_Char_feres_sd, replace
putexcel A2=matrix(PSStatsres),rownames


local ctr_mayor_demo $ctr_mayor_demo
local ctr_mayor_edu $ctr_mayor_edu
local ctr_mayor_poly $ctr_mayor_poly

local ctr_m_characteristics : list ctr_mayor_demo | ctr_mayor_edu
local ctr_m_characteristics: list ctr_m_characteristics | ctr_mayor_poly 
global ctr_m_characteristics `ctr_m_characteristics'
local nchar: word count $ctr_m_characteristics

tabstat $ctr_m_characteristics, statistics(mean sd count) save
return list
matrix MStats=r(StatTotal)'
putexcel set M_Char_sd, replace
putexcel A2=matrix(MStats),rownames


forvalues pp = 1/`nchar' {
local temp: word `pp' of $ctr_m_characteristics
xtreg `temp' i.year, fe cluster(id)
predict `temp'_m_feres, e
}

tabstat *_m_feres, statistics(mean sd count) save
return list
matrix MStatsres=r(StatTotal)'
putexcel set M_Char_res_sd, replace
putexcel A2=matrix(MStatsres),rownames

eststo clear
matrix AICmat=J(4,2,.)
matrix LAICmat=J(4,2,.)
matrix AICCmat=J(4,2,.)
matrix LAICCmat=J(4,2,.)
matrix BICmat=J(4,2,.)
matrix LBICmat=J(4,2,.)
matrix EBICmat=J(4,2,.)
matrix LEBICmat=J(4,2,.)
scalar c=0

// City and Year Fixed Effects LASSO and Elastic Net using Information Criteria to select Penalty Level
forvalues i=0.25 (0.25) 1 {
scalar c=c+1
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe
ereturn list

matrix AICmat[c,1]=`e(aicmin)'
matrix LAICmat[c,1]=`e(laic)'
matrix AICCmat[c,1]=`e(aiccmin)'
matrix LAICCmat[c,1]=`e(laicc)'
matrix BICmat[c,1]=`e(bicmin)'
matrix LBICmat[c,1]=`e(lbic)'
matrix EBICmat[c,1]=`e(ebicmin)'
matrix LEBICmat[c,1]=`e(lebic)'
lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe
ereturn list
matrix AICmat[c,2]=`e(aicmin)'
matrix LAICmat[c,2]=`e(laic)'
matrix AICCmat[c,2]=`e(aiccmin)'
matrix LAICCmat[c,2]=`e(laicc)'
matrix BICmat[c,2]=`e(bicmin)'
matrix LBICmat[c,2]=`e(lbic)'
matrix EBICmat[c,2]=`e(ebicmin)'
matrix LEBICmat[c,2]=`e(lebic)'

//ebic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(ebic) lic(ebic) postresults
ereturn list
global l2ebselp `e(selected)'
lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(ebic)  lic(ebic) postresults
ereturn list
global l2ebselm `e(selected)'

local l2ebselp $l2ebselp
local l2ebselm $l2ebselm
local l2ebsel : list l2ebselp | l2ebselm
global l2ebsel `l2ebsel'

//bic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(1) partial(time*) fe ic(bic) lic(bic) postresults
ereturn list
global l2bselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(bic)  lic(bic) postresults
ereturn list
global l2bselm `e(selected)'

local l2bselp $l2bselp
local l2bselm $l2bselm
local l2bsel : list l2bselp | l2bselm
global l2bsel `l2bsel'

//aicc
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(aicc) lic(aicc) postresults
ereturn list
global l2acselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(aicc)  lic(aicc) postresults
ereturn list
global l2acselm `e(selected)'

local l2acselp $l2acselp
local l2acselm $l2acselm
local l2acsel : list l2acselp | l2acselm
global l2acsel `l2acsel'

//aic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(aic) lic(aic) postresults
ereturn list
global l2aselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu time*,  alpha(`i') partial(time*) fe ic(aic)  lic(aic) postresults
ereturn list
global l2aselm `e(selected)'

local l2aselp $l2aselp
local l2aselm $l2aselm
local l2asel : list l2aselp | l2aselm
global l2asel `l2asel'

eststo clear
eststo: xtreg propFx $l2ebsel i.year, fe cluster(id)
eststo: xtreg propFx $l2bsel i.year, fe cluster(id)
eststo: xtreg propFx $l2acsel i.year, fe cluster(id)
eststo: xtreg propFx $l2asel i.year, fe cluster(id)
eststo: xtreg manipdays $l2ebsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2bsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2acsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2asel i.year, fe cluster(id)


esttab using "lasso2_alpha_`i'_ic_fe.tex", replace  b(3) noconstant drop(*.year)
}

matrix ICmat=AICmat,AICCmat,BICmat,EBICmat
putexcel set IC_matrix_elasticnet, replace
putexcel A2=matrix(ICmat)

///Baseline LASSO Control Variables (Province, City-rank and Year FE) with Penalty minimizing Information Criteria
// ebic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(ebic) lic(ebic) postresults
ereturn list
global l2gebselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(ebic)  lic(ebic) postresults
ereturn list
global l2gebselm `e(selected)'

local l2gebselp $l2gebselp
local l2gebselm $l2gebselm
local l2gebsel : list l2gebselp | l2gebselm
global l2gebsel `l2gebsel'


//bic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(bic) lic(bic) postresults
ereturn list
global l2gbselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(bic)  lic(bic) postresults
ereturn list
global l2gbselm `e(selected)'

local l2gbselp $l2gbselp
local l2gbselm $l2gbselm
local l2gbsel : list l2gbselp | l2gbselm
global l2gbsel `l2gbsel'


//aicc
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(aicc) lic(aicc) postresults
ereturn list
global l2gacselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(aicc)  lic(aicc) postresults
ereturn list
global l2gacselm `e(selected)'

local l2gacselp $l2gacselp
local l2gacselm $l2gacselm
local l2gacsel : list l2gacselp | l2gacselm
global l2gacsel `l2gacsel'


//aic
lasso2 propFx $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(aic) lic(aic) postresults
ereturn list
global l2gaselp `e(selected)'

lasso2 manipdays $ctr_sec_demo $ctr_sec_poly $ctr_sec_edu ///
$ctr_mayor_demo $ctr_mayor_poly $ctr_mayor_edu $dumsgroupfe,  alpha(1) partial($dumsgroupfe) ic(aic)  lic(aic) postresults
ereturn list
global l2gaselm `e(selected)'

local l2gaselp $l2gaselp
local l2gaselm $l2gaselm
local l2gasel : list l2gaselp | l2gaselm
global l2gasel `l2gasel'

eststo clear
eststo: xtreg propFx $l2gebsel i.year, fe cluster(id)
eststo: xtreg propFx $l2gbsel i.year, fe cluster(id)
eststo: xtreg propFx $l2gacsel i.year, fe cluster(id)
eststo: xtreg propFx $l2gasel i.year, fe cluster(id)

eststo: xtreg manipdays $l2gebsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2gbsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2gacsel i.year, fe cluster(id)
eststo: xtreg manipdays $l2gasel i.year, fe cluster(id)
esttab using "lasso_gfe_ic_results.tex", replace b(3) noconstant drop(*.year)

eststo clear

log close



